package com.jwsoft.manager.core.gateway.controller;

import com.alibaba.fastjson.JSON;
import com.bifang.engine.assistant.core.domain.AppResponse;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.protect.core.annotation.Idempotent;
import com.bifang.engine.protect.core.annotation.NoSignCrypto;
import com.bifang.module.base.common.vo.dict.SelectVO;
import com.bifang.module.base.common.vo.excel.ExcelReadVO;
import com.bifang.module.base.common.vo.excel.ExcelVO;
import com.bifang.module.base.core.utils.ExcelFileUtil;
import com.bifang.module.base.core.utils.ServiceRequestUtil;
import com.jwsoft.manager.common.vo.eduStudentEnroll.EduStudentEnrollExcelSaveVO;
import com.jwsoft.manager.common.vo.eduStudentEnroll.EduStudentEnrollVO;
import com.jwsoft.manager.common.vo.eduStudentEnroll.StudentEnrollFileVO;
import com.jwsoft.manager.core.integration.EduPolicyStudentImportIntegration;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author zjl
 * @version 1.0
 * @date 2024/7/5 0005 10:56
 */
@RestController
@RequestMapping(value = "/doc/api")
@Tag(name = "政策学生导入表", description = "政策学生导入表")
public class EduPolicyStudentImportController {
    @Autowired
    EduPolicyStudentImportIntegration eduPolicyStudentImportIntegration;
    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "招生计划模板下载（PC）",description = "招生计划模板下载")
    @PostMapping(value = "/eduPolicyStudentImport0001", produces = "application/json;charset=UTF-8")
    @NoSignCrypto
    public void downloadByCode(
            HttpServletRequest request,
            @RequestParam Map<String, Object> paramMap,
            @RequestBody(required = false) String body,
            HttpServletResponse response)  {
        ExcelVO vo = null;
        if (StringUtils.isNotBlank(body)) {
            vo = JSON.parseObject(body, ExcelVO.class);
        } else {
            vo = new ExcelVO();
            if (paramMap.get("modelCode") != null) {
                vo.setModelCode(paramMap.get("modelCode").toString());
            }
            if (paramMap.get("excelVersion") != null) {
                vo.setExcelVersion(paramMap.get("excelVersion").toString());
            }
            if (paramMap.get("rows") != null) {
                vo.setRows(Integer.parseInt(paramMap.get("rows").toString()));
            }
            if (paramMap.get("dictMap") != null) {
                vo.setDictMap(
                        (Map<String, List<SelectVO>>)
                                JSON.parse(JSON.toJSONString(paramMap.get("dictMap"))));
            }

        }
        vo.setAccessToken(ServiceRequestUtil.getAccessToken(request, response));
        if (StringUtils.isBlank(vo.getModelCode())) {
            throw new AppException("excel模板编号不能为空");
        }
        eduPolicyStudentImportIntegration.downloadModel(vo, response);
    }
    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "政策生文件上传（PC）",description = "政策生文件上传")
    @PostMapping(value = "/eduPolicyStudentImport0002", produces = "application/json;charset=UTF-8")
    @NoSignCrypto
    public AppResponse<ExcelReadVO> readByCode(
            HttpServletRequest request,
            @RequestParam Map<String, Object> paramMap,
            HttpServletResponse response) throws IOException {
        ExcelVO vo= ExcelFileUtil.getExcelVO(request,response,paramMap);
        MultipartFile file=ExcelFileUtil.getExcelFile(request);
        return AppResponse.successData(eduPolicyStudentImportIntegration.readExcel(vo, file));
    }
    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "政策生-excel导入保存",description = "政策生-excel导入保存")
    @PostMapping(value = "/eduPolicyStudentImport0003", produces = "application/json;charset=UTF-8")
    public AppResponse<EduStudentEnrollVO> add(@RequestBody EduStudentEnrollExcelSaveVO vo) {
        eduPolicyStudentImportIntegration.addExcel(vo);
        return AppResponse.successMsg("保存成功！");
    }
    @RequestMapping(
            value = "/eduPolicyStudentImport0004",
            method = {RequestMethod.POST, RequestMethod.GET})
    @Operation(summary = "政策生错误数据下载", description = "政策生错误数据下载")
    @NoSignCrypto
    public void downloadErrorExcel(
            HttpServletRequest request,
            @RequestParam Map<String, Object> paramMap,
            @RequestBody StudentEnrollFileVO vo,
            HttpServletResponse response) {
        if (StringUtils.isBlank(vo.getModelCode())) {
            throw new AppException("excel模板编号不能为空");
        }
        eduPolicyStudentImportIntegration.downloadErrorExcel(vo,response);
    }
}
